package com.fs.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fs.auth.TokenGenerator;
import com.fs.mapper.UserMapper;
import com.fs.mapper.UserTokenMapper;
import com.fs.pojo.User;
import com.fs.pojo.UserToken;
import com.fs.service.ShiroService;
import com.fs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("shiroservice")
public class ShiroServiceImpl implements ShiroService {
    //12小时后失效
    private final static int EXPIRE = 12;

    @Autowired
    private UserMapper userRepository;


    @Autowired
    private UserTokenMapper sysTokenRepository;

//    @Autowired
//    private SysAdminRolesMapper sysAdminRolesMapper;

//    @Autowired
//    private SysAdminMenusMapper sysAdminMenusMapper;


//    @Override
//    public User findByUsername(String username) {
//        return userRepository.findByUsername(username);
//    }

//    @Transactional
//    @Override
//    public ResultVO createToken(Integer userId) {
//        ResultVO result = new ResultVO();
//        String token = TokenGenerator.generateValue();
////        LocalDateTime now = LocalDateTime.now();
//        Date now = new Date();
////        LocalDateTime expireTime = now.plusHours(EXPIRE);
//        DateTime expireTime = DateUtil.offsetHour(now, EXPIRE);
//        UserToken tokenEntity= sysTokenRepository.findByUserId(userId);
//        if(tokenEntity==null){
//            tokenEntity=new UserTokenEntity();
//            tokenEntity.setUserId(userId);
//            tokenEntity.setToken(token);
//            tokenEntity.setUpdated(now);
//            tokenEntity.setEndTime(expireTime);
//        }else{
//            tokenEntity.setToken(token);
//            tokenEntity.setUpdated(now);
//            tokenEntity.setEndTime(expireTime);
//        }
//        sysTokenRepository.insert(tokenEntity);
//        Map<String,Object> map=new HashMap<>();
//        map.put("token",token);
//        map.put("expire",expireTime);
//        result.setData(map);
//        return result;
//    }

    @Override
    public UserToken findByToken(String accessToken) {
        return sysTokenRepository.findByToken(accessToken);

    }

    @Override
    public User findByUserId(Integer userId) {
        User admin = userRepository.selectById(userId);
//        List<SysAdminRolesEntity> list = new ArrayList<>();
//        //超级管理员
//        if(userId==1){
//            SysAdminRolesEntity rolesEntity = new SysAdminRolesEntity();
//            rolesEntity.setRoleName("超级管理员")
//                    .setPermissions(sysAdminMenusDao.selectList(null));
//
//            list.add(rolesEntity);
//            return admin.setRoles(list);
//        }
//        // 其他角色
//        List<SysAdminRolesEntity> roles=sysAdminRolesDao.selectByUserId(userId);
//        admin.setRoles(roles);
//
//        for (SysAdminRolesEntity role : roles) {
//            role.setPermissions(sysAdminMenusDao.selectMenus(role.getId()));
//        }
        return admin;
    }

    @Transactional
    @Override
    public void logout(String token) {
        UserToken byToken = findByToken(token);
        //生成一个token
        token = TokenGenerator.generateValue();
        //修改token
        byToken.setToken(token).setUpdated(new Date());
        //使前端获取到的token失效
        sysTokenRepository.updateById(byToken);
    }
}
